package com.orangelife.mobile.housepurchasing.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.http.OrangeHttpBiz;
import com.curry.android.http.VolleyHttpUtil;

import com.curry.orangelife.mobile.R;
import com.markupartist.android.widget.PullToRefreshListView;

import com.orangelife.mobile.common.activity.PullRefreshBaseActivity;
import com.orangelife.mobile.common.view.EmptyLayout;

import com.orangelife.mobile.housepurchasing.adapter.SelectCityAdapter;
import com.orangelife.mobile.housepurchasing.biz.HouseListHttpBiz;

import com.orangelife.mobile.login.biz.CommunityBiz;
import com.orangelife.mobile.login.biz.CommunityHttpBiz;


import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * Created by HeYuCan on 2016/5/24.
 */
public class SelectCityActivity extends PullRefreshBaseActivity {
    /**
     * ---------------------选择城市页面------------------------
     */
    @Bind(R.id.lvHouseLocation)
    ListView lvHouseLocation;
    @Bind(R.id.ll_select_city)
    LinearLayout llSelectCity;
    @Bind(R.id.ll_select_comm)
    LinearLayout llSelectComm;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.pr_commList)
    PullToRefreshListView pullListView;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    @Bind(R.id.emptyLayout_city)
    EmptyLayout cEmptyLayout;

    private SelectCityAdapter<Map<String, Object>> selectCityAdapter;
    private SelectCommAdapter<Map<String, Object>> selectCommAdapter;

    private String cityName=null;
    private String commName=null;
    private List<Map<String,Object>> cityList = new ArrayList<>();
    private List<Map<String,Object>> commList = new ArrayList<>();
    private String commID=null;

    /**
     * -------------选择小区页面------------------------
     */





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property_comm);
        ButterKnife.bind(this);
        initView();
        setAdapter();


    }

    private void setAdapter() {
        selectCityAdapter = new SelectCityAdapter<>(this, cityList);

        selectCommAdapter = new SelectCommAdapter<>(this, commList);
        lvHouseLocation.setAdapter(selectCityAdapter);
        setXListViewAndEmptyLayoutListener(pullListView, emptyLayout, selectCommAdapter);

    }






    private void requestList() {

        HouseListHttpBiz.requestCityList(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                cityList = HouseListHttpBiz.getCityList(response);
                selectCityAdapter.setList(cityList);
                dismissEmptyLayout2();
            }
        });
    }



    //----------------------------------------以下为小区选择的页面----------------------------------------------------------
    //----------------------------------------以下为小区选择的页面----------------------------------------------------------

    private void initView() {
        Intent intent =getIntent();
        if(!intent.getStringExtra("city").equals("")){
            cityName=intent.getStringExtra("city");
            tvTitle.setText("小区");
            llSelectCity.setVisibility(View.GONE);
            llSelectComm.setVisibility(View.VISIBLE);

            dismissEmptyLayout2();
            requestWhenPullRefresh();
        }else {
            tvTitle.setText("城市");
            requestList();
        }
        lvHouseLocation.setOnItemClickListener(itemListener);

    }

    private void dismissEmptyLayout2() {
        if (getEmptyLayout2() != null) {
            getEmptyLayout2().dismiss();
        }
    }

    private     EmptyLayout getEmptyLayout2() {
        return cEmptyLayout;
    }

    /**
     * 点击跳转，并保存小区信息
     */
    private AdapterView.OnItemClickListener itemListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

            if (position == -1) {
                return;
            }
            switch (adapterView.getId()) {
                case R.id.lvHouseLocation:
                    llSelectCity.setVisibility(View.GONE);
                    llSelectComm.setVisibility(View.VISIBLE);
                    cityName = (String)cityList.get(position).get("city");
                    tvTitle.setText("小区");
                    requestWhenPullRefresh();

                    break;

                default:
                    break;
            }
        }
    };


    @Override
    protected EmptyLayout getEmptyLayout() {
        return emptyLayout;
    }

    @Override
    protected List getData() {
        return commList;
    }

    @Override
    protected int getDataCount() {
        return commList == null ? 0 : commList.size();
    }

    /** 滚动获取小区列表 */
    @Override
    protected void requestWhenPullRefresh() {
        if(cityName==null&"".equals(cityName)){
            return;
        }
            /*latitude!=null,说明从发布投诉的界面传入的intent附加了latitude,说明用户点击了选择小区，。以下为获取某城市小区列表*/
        CommunityHttpBiz.getCommunityList(cityName, null, null, null, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                ArrayList data = (ArrayList) CommunityHttpBiz.handleCommunityList(response);
                handleAfterRequest(isConnected(false), OrangeHttpBiz.getTotalNum(response), data, pullListView, selectCommAdapter);
            }
        }, pageNo, pageSize);


    }


    public void onBackClick(View v){
        finish();
    }


      class SelectCommAdapter <T> extends FatherAdapter<T> {

        private Context mContext;
        private LayoutInflater listContainer;
        private List<Map<String,Object>> mList = null;


        class ViewHolder {
            public TextView tv_item_name;
            public TextView tv_address;
            public RelativeLayout rl_Click;
        }


        public SelectCommAdapter(Context context,
                                 List<Map<String,Object>> list) {
            this.mContext = context;
            listContainer = LayoutInflater.from(mContext);
            this.mList = list;
        }



        @Override
        public int getCount() {
            if (mList == null) {
                return 0;
            }
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = listContainer.inflate(
                        R.layout.item_lv_select_cityorcomm, null);
                holder.tv_item_name = (TextView) convertView
                        .findViewById(R.id.tv_community_name);
                holder.tv_address =(TextView) convertView
                        .findViewById(R.id.tv_community_address);
                holder.rl_Click=(RelativeLayout) convertView
                        .findViewById(R.id.rl_comm);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Map<String,Object> map=mList.get(position);
            String commName=String.valueOf(map.get("comm"));
            String address=String.valueOf(map.get("stree"));
            holder.rl_Click.setOnClickListener(new ClickListener(map));
            holder.tv_item_name.setText(commName);
            holder.tv_address.setText(address);
            holder.tv_address.setVisibility(View.VISIBLE);
            return convertView;
        }


        @Override
        public BaseAdapter setList(List<T> list) {
            this.mList = (List<Map<String, Object>>) list;
            return SelectCommAdapter.this;
        }

        class ClickListener implements View.OnClickListener{

            private Map<String,Object> map=null;
            public  ClickListener(Map<String,Object> map){
                this.map=map;
            }
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                commName = String.valueOf(map.get("comm"));
                String commID=String.valueOf(map.get("commID"));
                intent.putExtra("cityName", cityName);
                intent.putExtra("commName", commName);
                intent.putExtra("commID", commID);
                setResult(0, intent);

                SelectCityActivity.this.finish();
            }
        }
    }
}
